package iclass;

import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import superman.app.gr100.biz.gr_expapp.MainActivity;

/**
 * Created by Scor on 2015/12/7.
 */
public class Itools {
    private String result;
    public String  httprequest(final String urlStr,final Object objects){
        Thread thread = new Thread(new Runnable(){
            public void run() {
                try{
                    URL url = new URL(urlStr);
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("POST");
                    urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                    urlConnection.connect();
                    DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());

                    Log.e("jsonObject", objects.toString());
                    out.write(objects.toString().getBytes("UTF-8"));
                    out.flush();
                    out.close();
                    InputStreamReader in = new InputStreamReader(urlConnection.getInputStream());
                    BufferedReader bufferedReader = new BufferedReader(in);
                    String readLine;
                    StringBuilder stringBuilder = new StringBuilder();
                    while ((readLine = bufferedReader.readLine()) != null) {
                        stringBuilder.append(readLine);
                    }
                    result=stringBuilder.toString();
                    in.close();
                    urlConnection.disconnect();
                }catch(Exception e){
                }
            }
        });
        thread.start();
        try { thread.join(); } catch (Exception e) {return "not join";}
        if(result !=null){
            return result ;
        }else {
            return null;
        }
    }
}
